import { join } from 'path'

import { genDts } from '@byted/dts-from-dsl'
import { Command } from 'clipanion'
import { fdir } from 'fdir'

const Projects = [
  {
    root: join('packages', 'job-runner', 'src'),
    outDir: 'idl',
    input: 'idl',
  },
]

export class IdlCommand extends Command {
  static paths = [['idl']]

  static async execute() {
    for (const project of Projects) {
      const api = new fdir().withFullPaths().crawl(join(process.cwd(), project.root, project.input))
      const files = (await api.withPromise()) as string[]
      const idlFiles = files.filter((idl) => idl.endsWith('.thrift'))
      genDts(idlFiles, {
        rootDir: join(process.cwd(), project.root, project.input),
        outDir: join(process.cwd(), project.root, project.outDir),
        target: 'es6',
        prettier: true,
      })
    }
  }

  async execute() {
    await IdlCommand.execute()
  }
}
